#import "AppDelegate.h"
#import "GeneratedPluginRegistrant.h"
#import "utils/WifiUtils.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [GeneratedPluginRegistrant registerWithRegistry:self];
    // Override point for customization after application launch.
    _controller = (FlutterViewController*)self.window.rootViewController;
    
    self.aioHelper = [[PWAIOBlueHelper alloc] init];
    FlutterMethodChannel* AIOChannel = [FlutterMethodChannel
                                            methodChannelWithName:@"com.flexolink.wch/aio/scan"
                                            binaryMessenger:_controller];
    [self.aioHelper setMethodChannel:AIOChannel];
    
    ///接收到原生发来的开启定位获取 wifi 名称 通道
    FlutterMethodChannel* locationChannel = [FlutterMethodChannel
                                            methodChannelWithName:@"com.flexolink.wch/aio/location"
                                            binaryMessenger:_controller];
    
    [locationChannel setMethodCallHandler:^(FlutterMethodCall * _Nonnull call, FlutterResult  _Nonnull result) {
        [self startUpdateLocation];
    }];
    
    
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

-(CLLocationManager*)locationManager{
    if (!_locationManager) {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        _locationManager.distanceFilter = kCLLocationAccuracyThreeKilometers;
        [_locationManager requestWhenInUseAuthorization];
    }
    return _locationManager;
}

- (void) startUpdateLocation {
    if (self.isStartLocation) return;
    self.isStartLocation = YES;
    [self.locationManager startUpdatingLocation];
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    [self.aioHelper setWifiText:[WifiUtils wifiName]];
}

@end
